<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>es6内容-类和方法 | LYH的博客</title><meta name="author" content="Y"><meta name="copyright" content="Y"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="es6内容-类和方法&#96;class类新的class写法只是让对象原型的写法更加清晰更像面向对象编程，通过class关键字，可以定义类。 构造函数：constructor方法是类的默认方法，通过new命令生成对象实例时，自动调用该方法。一个类必须有constructor方法，如果没有显式定义，一个空的constructor方法会被默认添加。 实例属性和方法：定义在类体中的属性称为实例属性，定义在类体中">
<meta property="og:type" content="article">
<meta property="og:title" content="es6内容-类和方法">
<meta property="og:url" content="https://gitee.com/liu-yinghui-123/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/index.html">
<meta property="og:site_name" content="LYH的博客">
<meta property="og:description" content="es6内容-类和方法&#96;class类新的class写法只是让对象原型的写法更加清晰更像面向对象编程，通过class关键字，可以定义类。 构造函数：constructor方法是类的默认方法，通过new命令生成对象实例时，自动调用该方法。一个类必须有constructor方法，如果没有显式定义，一个空的constructor方法会被默认添加。 实例属性和方法：定义在类体中的属性称为实例属性，定义在类体中">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/liu-yinghui-123/lyh/img/3.jpg">
<meta property="article:published_time" content="2022-09-09T09:10:06.344Z">
<meta property="article:modified_time" content="2022-09-13T06:15:58.565Z">
<meta property="article:author" content="Y">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/liu-yinghui-123/lyh/img/3.jpg"><link rel="shortcut icon" href="/lyh/img/favicon.png"><link rel="canonical" href="https://gitee.com/liu-yinghui-123/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/lyh/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/lyh/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'es6内容-类和方法',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-09-13 14:15:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/static-butterfly/dist/css/index.min.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@1,300&display=swap" rel="stylesheet"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/lyh/img/tx.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/lyh/archives/"><div class="headline">Articles</div><div class="length-num">3</div></a><a href="/lyh/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/lyh/categories/"><div class="headline">Categories</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/lyh/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></div><div class="menus_item"><a class="site-page" href="/lyh/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/lyh/img/3.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/lyh/">LYH的博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/lyh/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></div><div class="menus_item"><a class="site-page" href="/lyh/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">es6内容-类和方法</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2022-09-09T09:10:06.344Z" title="Created 2022-09-09 17:10:06">2022-09-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2022-09-13T06:15:58.565Z" title="Updated 2022-09-13 14:15:58">2022-09-13</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="es6内容-类和方法"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><strong>es6内容-类和方法</strong>`<br>class类<br>新的class写法只是让对象原型的写法更加清晰更像面向对象编程，通过class关键字，可以定义类。</p>
<p>构造函数：constructor方法是类的默认方法，通过new命令生成对象实例时，自动调用该方法。一个类必须有constructor方法，如果没有显式定义，一个空的constructor方法会被默认添加。</p>
<p>实例属性和方法：定义在类体中的属性称为实例属性，定义在类体中的方法称为实例方法。</p>
<p>不推荐在实例上定义方法，因为这样做方法会创建在实例上。</p>
<p>原型属性和方法：原型属性和方法顾名思义就是在构造函数的原型对象上存在的属性和方法。</p>
<p>不推荐在实例上定义属性，因为这样做属性的值无法自定义。</p>
<p>静态属性和方法：通过<code>static</code>关键字来定义静态属性和静态方法。静态属性和静态方法是定义在类上的，所以可以通过类直接访问。在静态方法中，<code>this</code>指向当前类。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span>&#123;</span><br><span class="line"><span class="comment">//静态方法</span></span><br><span class="line"><span class="keyword">static</span> num = <span class="number">0</span>;</span><br><span class="line"><span class="comment">//静态方法</span></span><br><span class="line"><span class="keyword">static</span> <span class="title function_">getNum</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Animal</span>.<span class="property">num</span>);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="comment">//构造函数</span></span><br><span class="line"><span class="title function_">constructor</span>(<span class="params">name,age</span>)&#123;</span><br><span class="line"><span class="comment">//实例属性</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">age</span> = age;</span><br><span class="line"><span class="comment">//实例方法不推荐在这里创建，每个实例创建都会被重复创建</span></span><br><span class="line">    <span class="title class_">Animal</span>.<span class="property">num</span>++;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="comment">//原型方法(Animal.prototype.sayName)</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">sayName</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> d1 = <span class="keyword">new</span> <span class="title class_">Animal</span>(<span class="string">&#x27;xiaobai&#x27;</span>,<span class="number">2</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(d1);<span class="comment">//Animal &#123; name: &#x27;xiaobai&#x27;, age: 2 &#125;</span></span><br><span class="line">d1.<span class="title function_">sayName</span>()<span class="comment">//xiaobai</span></span><br><span class="line"><span class="title class_">Animal</span>.<span class="title function_">getName</span>();<span class="comment">//1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Animal</span>.<span class="property">num</span>)<span class="comment">//静态属性和方法只能通过类名调用</span></span><br></pre></td></tr></table></figure>




<p>继承：可以通过<code>extends</code>关键字实现继承，子类中可以没有构造函数，系统会默认提供一个。子类如果提供了构造函数就必须显示调用super。super函数类似于之前的借用构造函数。</p>
<p>class 作为构造函数的语法糖，同时有prototype属性和__proto__属性，因此同时存在两条继承链</p>
<p>1.子类的__proto__属性，表示构造函数的继承，总是指向父类。</p>
<p>2.子类prototype属性的__proto__属性，表示方法的继承，总是指向父类的prototype属性。</p>
<p>super 关键字</p>
<p>super作为函数调用时，代表父类的构造函数。</p>
<p>super作为对象时，在普通方法中，指向父类的原型对象</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span>&#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">name,age</span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">age</span> = age;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//原型方法</span></span><br><span class="line">    <span class="title function_">sayName</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>);</span><br><span class="line">&#125;</span><br><span class="line">    <span class="title function_">sayAge</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">name</span>+<span class="variable language_">this</span>.<span class="property">age</span>+<span class="string">&#x27;岁了!&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//继承</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Animal</span>&#123;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">name,age,color</span>)&#123;</span><br><span class="line">        <span class="variable language_">super</span>(name,age);<span class="comment">//super作为对象，指向父类的原型属性</span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">color</span> = color;</span><br><span class="line">&#125;</span><br><span class="line">    <span class="title function_">sayColor</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">color</span> = color</span><br><span class="line">&#125;</span><br><span class="line">    <span class="title function_">sayAge</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">super</span>.<span class="title function_">sayAge</span>();<span class="comment">//super作为函数，指向父类的原型函数</span></span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> d1 = <span class="keyword">new</span> <span class="title class_">Dog</span>(<span class="string">&#x27;xiaoming&#x27;</span>,<span class="number">2</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(d1);<span class="comment">//Dog &#123; name: &#x27;xiaobai&#x27;, age: 2, color: &#x27;red&#x27; &#125;</span></span><br><span class="line">d1.<span class="title function_">sayName</span>();<span class="comment">//xiaoming</span></span><br><span class="line">d1.<span class="title function_">sayAge</span>();<span class="comment">//xioaming2岁了!</span></span><br><span class="line"> </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Dog</span>.<span class="property">__proto__</span>);<span class="comment">//[class Animal] 继承构造函数，总是指向父类</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Dog</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">__proto__</span> === <span class="title class_">Aniaml</span>.<span class="property"><span class="keyword">prototype</span></span>)<span class="comment">//true 继承原型方法，子类prototype属性的__proto__属性，总是指向父类的prototype属性。</span></span><br></pre></td></tr></table></figure>



<p>迭代器<br>迭代器/遍历器（Iterator）就是这样一种机制。它是一种接口，为各种不同的数据结构提供统一的访问机制。</p>
<p>如果我们想自定义一个迭代器，必须为对象添加一个Symbol.iterator方法，这个方法必须返回一个 迭代器（iterator） —— 内部一个有next方法的对象。</p>
<p>原始就具有迭代器接口的数据类型有：Array、Map、Set、String、TypedArray、arguments、NodeList</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]<span class="comment">//可以使用for-of循环</span></span><br><span class="line"><span class="keyword">var</span> itr =  arr[<span class="title class_">Symbol</span>.<span class="property">iterator</span>]()<span class="comment">//返回迭代器对象</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 2, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 3, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 4, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: undefined, done: true &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: undefined, done: true &#125;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//自定义一个可迭代对象</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    <span class="attr">data</span>:[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],</span><br><span class="line">    [<span class="title class_">Symbol</span>.<span class="property">iterator</span>]()&#123;</span><br><span class="line">        <span class="keyword">var</span> index = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">var</span> _this = <span class="variable language_">this</span>;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="title function_">next</span>(<span class="params"></span>)&#123;</span><br><span class="line">                <span class="keyword">if</span>(index&lt;_this.<span class="property">data</span>.<span class="property">length</span>)&#123;</span><br><span class="line">                    <span class="keyword">return</span> &#123; <span class="attr">value</span>:_this.<span class="property">data</span>[index++], <span class="attr">done</span>:<span class="literal">false</span>&#125;</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="keyword">return</span>&#123; <span class="attr">value</span>:<span class="literal">undefined</span>,<span class="attr">done</span>:<span class="literal">true</span>&#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;         </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> itr = obj[<span class="title class_">Symbol</span>.<span class="property">iterator</span>]();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 2, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 3, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value: 4, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value:undefined,done:true&#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(itr.<span class="title function_">next</span>());<span class="comment">//&#123; value:undefined,done:true&#125;</span></span><br></pre></td></tr></table></figure>




<p>promise<br>Promise 是异步编程的一种解决方案。简单说就是一个容器，里面保存着某个未来才会发生的事件（通常是一个异步操作）的结果。</p>
<p>Promise对象代表一个异步操作有三种状态： <strong>pending（进行中，初始状态）</strong>、<strong>fulfilled（已成功）</strong>和<strong>rejected（已失败）</strong>。</p>
<p>状态发生改变之后就凝固了，不会再变了，会一直保持这个结果，这时就称为 <strong>resolved（已定型）</strong>。</p>
<p>原型方法</p>
<p>Promise.prototype.then() 当状态由pending变为fulfilled的时候执行该回调函数</p>
<p>Promise.prototype.catch() 当状态由pending变为rejected的时候执行该回调函数</p>
<p>Promise.prototype.finally() 在promise结束时，无论结果是fulfilled或者是rejected，都会执行指定的回调函数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pm = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="title function_">setTomeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()&gt;<span class="number">0.1</span>)&#123;resolve<span class="string">&#x27;下雨了&#x27;</span>&#125;</span><br><span class="line">        <span class="keyword">else</span>&#123;reject<span class="string">&#x27;没下雨&#x27;</span>&#125;</span><br><span class="line">    &#125;，<span class="number">3000</span>)</span><br><span class="line">&#125;)</span><br><span class="line">pm.<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">&#125;).<span class="title function_">finally</span>(<span class="function">()=&gt;</span>&#123;<span class="comment">//无论是够成功与否，都会执行</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;测试了一次&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>


<p>Promise.all([p1,p2]) 当p1,p2状态都为fulfilled(成功)时候，该实例的状态才为fulfilled </p>
<p>Promise.any([p1,p2]) 只要p1,p2状态有一个变为fulfilled，该实例的状态为fulfilled</p>
<p>Promise.race([p1,p2]) 当p1，p2之中有一个实例率先改变状态，该实例的状态就跟着改变。哪个承诺先成功就执行哪个(竞速)</p>
<p>Promise.allSettled() ES11新增，所有的承诺都执行完毕后，返回结果数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pm = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="title function_">setTomeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()&gt;<span class="number">0.1</span>)&#123;resolve<span class="string">&#x27;下雨了&#x27;</span>&#125;</span><br><span class="line">        <span class="keyword">else</span>&#123;reject<span class="string">&#x27;没下雨&#x27;</span>&#125;</span><br><span class="line">    &#125;，<span class="number">3000</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> pm1 = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="title function_">setTomeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()&gt;<span class="number">0.1</span>)&#123;resolve<span class="string">&#x27;打雷了&#x27;</span>&#125;</span><br><span class="line">        <span class="keyword">else</span>&#123;reject<span class="string">&#x27;没打雷&#x27;</span>&#125;</span><br><span class="line">    &#125;，<span class="number">3000</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">All</span>_pm = <span class="title class_">Promise</span>.<span class="title function_">all</span>([pm,pm1]);</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">All</span>_pm = <span class="title class_">Promise</span>.<span class="title function_">any</span>([pm,pm1]);</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">All</span>_pm = <span class="title class_">Promise</span>.<span class="title function_">race</span>([pm,pm1]);</span><br><span class="line"><span class="keyword">var</span> <span class="title class_">All</span>_pm = <span class="title class_">Promise</span>.<span class="title function_">allSettled</span>([pm,pm1]);<span class="comment">//[</span></span><br><span class="line"><span class="comment">//  &#123; status: &#x27;fulfilled&#x27;, value: &#x27;下雨了&#x27; &#125;,</span></span><br><span class="line"><span class="comment">//  &#123; status: &#x27;fulfilled&#x27;, value: &#x27;打雷了&#x27; &#125;</span></span><br><span class="line"><span class="comment">//]</span></span><br><span class="line"><span class="title class_">All</span>_pm.<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">&#125;).<span class="title function_">finally</span>(<span class="function">()=&gt;</span>&#123;<span class="comment">//无论是够成功与否，都会执行</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;测试了一次&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<p>Promise.resolve() 用于将现有对象转化为Promise实例fulfilled状态 </p>
<p>Promise.reject() 用于将现有对象转化为Promise实例rejected状态</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> res1 = <span class="title class_">Promise</span>.<span class="title function_">resolve</span>(<span class="string">&#x27;ok&#x27;</span>)</span><br><span class="line">res1.<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)<span class="comment">//ok</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">var</span> res2 = <span class="title class_">Promise</span>.<span class="title function_">reject</span>(<span class="keyword">new</span>.<span class="title class_">Error</span>(<span class="string">&#x27;出错&#x27;</span>));</span><br><span class="line">res2.<span class="title function_">catch</span>(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">&#125;)<span class="comment">//Error: 出错</span></span><br></pre></td></tr></table></figure>



<p>Generator<br>Generator 函数是 ES6 提供的一种异步编程解决方案，语法行为与传统函数完全不同</p>
<p>Generator 函数有多种理解角度。语法上，首先可以把它理解成，Generator 函数是一个状态机，封装了多个内部状态。执行 Generator 函数会返回一个遍历器对象，可以依次遍历 Generator 函数内部的每一个状态。</p>
<p>特点</p>
<p>function关键字与函数名之间有个星号</p>
<p>函数内部使用yield表达式</p>
<p>可以放置return语句，返回最终的结果</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span>* <span class="title function_">getData</span>(<span class="params"></span>)&#123;</span><br><span class="line">     <span class="keyword">yield</span> <span class="title function_">f1</span>();</span><br><span class="line">     <span class="keyword">yield</span> <span class="title function_">f2</span>();</span><br><span class="line">     <span class="keyword">yield</span> <span class="title function_">f3</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = <span class="title function_">getData</span>();</span><br><span class="line">res.<span class="title function_">next</span>()</span><br><span class="line">交替执行</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">f1</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">f2</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">function</span>* <span class="title function_">getNum</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">while</span>(<span class="literal">true</span>)&#123;</span><br><span class="line">        <span class="keyword">yield</span> <span class="title function_">f1</span>();</span><br><span class="line">        <span class="keyword">yield</span> <span class="title function_">f2</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = <span class="title function_">getNum</span>();</span><br><span class="line"><span class="built_in">setInterval</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="title function_">next</span>());</span><br><span class="line">&#125;,<span class="number">1000</span>)<span class="comment">//1 2 1 2 1 2...</span></span><br><span class="line"></span><br><span class="line">顺序执行</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">xy</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()&gt;<span class="number">0.1</span>)&#123;resolve<span class="string">&#x27;下雨了&#x27;</span>&#125;</span><br><span class="line">            <span class="keyword">else</span>&#123;reject<span class="string">&#x27;没有下雨&#x27;</span>&#125;</span><br><span class="line">        &#125;，<span class="number">3000</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span>* <span class="title function_">getData</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">yield</span> <span class="string">&#x27;xiaoming&#x27;</span></span><br><span class="line">    <span class="keyword">yield</span> <span class="title function_">xy</span>()</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;没有了&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = <span class="title function_">getData</span>();<span class="comment">//返回遍历器对象</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="title function_">next</span>());<span class="comment">//&#123; value: &#x27;xiaoming&#x27;, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="title function_">next</span>());<span class="comment">//&#123; value: Promise &#123; &lt;pending&gt; &#125;, done: false &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="title function_">next</span>());<span class="comment">//&#123; value: &#x27;没有了&#x27;, done: true &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="title function_">next</span>());<span class="comment">//&#123; value: undefined, done: true &#125;</span></span><br></pre></td></tr></table></figure>



<p>Async [əˈzɪŋk]异步编程<br>Async函数是generator函数的语法糖，在generator函数的基础上添加了一些更加方便用户操作的新特性。Async函数的执行和普通函数一致，只需要一行代码即可，因为他具有内置的执行。async与await对比*与yield有更好的语义</p>
<p>特点：</p>
<p>await关键字只在async函数内有效。</p>
<p>async函数返回一个 Promise 对象。</p>
<p>async函数可以实现也是一种异步函数同步化的实现</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">xy</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">//预测3秒后会下雨</span></span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>()&gt;<span class="number">0.1</span>)&#123;</span><br><span class="line">            <span class="title function_">resolve</span>(<span class="string">&#x27;下雨了&#x27;</span>)</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="title function_">reject</span>(<span class="string">&#x27;没有下雨&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,<span class="number">3000</span>)</span><br><span class="line">&#125;);<span class="comment">//异步</span></span><br><span class="line">&#125;</span><br><span class="line">(<span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">getData</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="keyword">var</span> res = <span class="keyword">await</span> <span class="title function_">xy</span>();</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;</span><br><span class="line">)<span class="comment">//下雨了</span></span><br></pre></td></tr></table></figure>



<p> proxy/reflect<br>proxy我们可以理解为代理，其含义是在对象操作时，我们可以做一些额外的操作；例如对数据的处理，对构造函数的处理，对数据的验证。直接利用Reflect对象直接获取Proxy的默认值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&#x27;xiaoming&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">obj = <span class="keyword">new</span> <span class="title class_">Proxy</span>(obj,&#123;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">target,key</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;huang&#x27;</span>+target[key]</span><br><span class="line">&#125;;</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">target,key,value</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="title class_">Reflect</span>.<span class="title function_">set</span>(target,key,value)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj.<span class="property">name</span>)<span class="comment">//huangxiaoming</span></span><br><span class="line">ajax</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">get</span>(<span class="params">url</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> x = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>;</span><br><span class="line">    x.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>,url,<span class="literal">true</span>);</span><br><span class="line">    x.<span class="title function_">send</span>(<span class="literal">null</span>);</span><br><span class="line">    x.<span class="property">onreadystatechange</span> =<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(x.<span class="property">readyState</span> == <span class="number">4</span> &amp;&amp;x.<span class="property">status</span>==<span class="number">200</span>)&#123;</span><br><span class="line">            <span class="title function_">resolve</span>(x.<span class="property">responseText</span>)</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(x.<span class="property">status</span>!=<span class="number">200</span>)&#123;</span><br><span class="line">            <span class="title function_">reject</span>(<span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&#x27;请求失败！&#x27;</span>))</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> url = <span class="string">&quot;http://81.69.24.232/pet/getArr.php&quot;</span>;</span><br><span class="line">    (<span class="keyword">async</span> <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">var</span> res = <span class="keyword">await</span> <span class="title function_">get</span>(url);<span class="comment">//等承诺执行完毕</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(res);</span><br><span class="line"></span><br></pre></td></tr></table></figure>




</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://gitee.com/liu-yinghui-123/lyh">Y</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://gitee.com/liu-yinghui-123/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/">https://gitee.com/liu-yinghui-123/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="/lyh/img/3.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/lyh/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/lyh/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/lyh/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/lyh/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/lyh/2022/09/07/My-New-Post/"><img class="next-cover" src="" onerror="onerror=null;src='/lyh/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">My New Post</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/lyh/img/tx.jpg" onerror="this.onerror=null;this.src='/lyh/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Y</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/lyh/archives/"><div class="headline">Articles</div><div class="length-num">3</div></a><a href="/lyh/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/lyh/categories/"><div class="headline">Categories</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/" title="es6内容-类和方法"><img src="/lyh/img/3.jpg" onerror="this.onerror=null;this.src='/lyh/img/404.jpg'" alt="es6内容-类和方法"/></a><div class="content"><a class="title" href="/lyh/2022/09/09/%E3%80%90es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%E3%80%91es6%E5%86%85%E5%AE%B9-%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95/" title="es6内容-类和方法">es6内容-类和方法</a><time datetime="2022-09-09T09:10:06.344Z" title="Created 2022-09-09 17:10:06">2022-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/lyh/2022/09/07/My-New-Post/" title="My New Post"><img src="" onerror="this.onerror=null;this.src='/lyh/img/404.jpg'" alt="My New Post"/></a><div class="content"><a class="title" href="/lyh/2022/09/07/My-New-Post/" title="My New Post">My New Post</a><time datetime="2022-09-07T07:30:30.000Z" title="Created 2022-09-07 15:30:30">2022-09-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/lyh/2022/09/07/hello-world/" title="Hello World"><img src="" onerror="this.onerror=null;this.src='/lyh/img/404.jpg'" alt="Hello World"/></a><div class="content"><a class="title" href="/lyh/2022/09/07/hello-world/" title="Hello World">Hello World</a><time datetime="2022-09-07T07:06:41.831Z" title="Created 2022-09-07 15:06:41">2022-09-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/lyh/img/3.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Y</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button id="chat_btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/lyh/js/utils.js"></script><script src="/lyh/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'XqXQeSqtjlUpx7pgmSvpS1T5-gzGzoHsz',
      appKey: 'hbLLncW9AsxRa0Ajg1AJnCmA',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="60198" data-server="netease" data-order="random" data-type="playlist" data-fixed="true" data-autoplay="true"> </div> <script async src="https://npm.elemecdn.com/tzy-blog/lib/js/other/sakura.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="I,LOVE,YOU" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>